package mes.operator.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import mes.operator.dao.OperatorDAO;
import mes.operator.dto.ComponentDTO;
import mes.operator.dto.EmployeeDTO;
import mes.operator.dto.MachineDTO;
import mes.operator.dto.HistoryDTO;
import mes.utility.Utility;

public class OperatorManageServiceImpl implements OperatorManageService{
	private OperatorDAO dao;

	public OperatorManageServiceImpl(OperatorDAO dao){
		this.dao = dao;
	}
	@Override
	public void OperatorManageServiceImpl(OperatorDAO dao) {
	}

	@Override
	public EmployeeDTO login(EmployeeDTO dto) throws SQLException {
		return dao.login(dto);
	}

	public int getComponentCount(String component_name) throws SQLException{
		ArrayList<ComponentDTO> list = dao.getComponentCount(component_name);
		return list.size();
	}
	
	public void insertComponent(ComponentDTO cdto) throws SQLException{
		double num = Math.random()*10;
		if(num <= 2){
			cdto.setIs_fault("Y");
		}else{
			cdto.setIs_fault("N");
		}
		dao.insertComponent(cdto);
	}
	@Override
	public ArrayList<ComponentDTO> getComponentInfo(String component_name, int cnt) throws SQLException {
		return dao.getComponentInfo(component_name, cnt);
	}
	@Override
	public void setMachineAInfo(ArrayList<ComponentDTO> componentList, int i) throws SQLException {
		HashMap map = new HashMap();
		String time = Utility.getNow();
		map.put("time", time);
		map.put("component_id", componentList.get(i).getComponent_id());
		map.put("machine", "machine_a");
		double num = 0;
		num = Math.random()*100;
		if(num <= 20){
			componentList.get(i).setIs_fault("Y");
			map.put("is_fault", "Y");
			map.put("faulty_line", "A");
			dao.insertHistory(map);				
		}else{
			map.put("is_fault", "N");
			map.put("faulty_line", " ");
			componentList.get(i).setIs_fault("N");
			dao.insertHistory(map);	
		}
		dao.updateComponentInfo(componentList.get(i));
		
		dao.insertMachineA(map);
		if(i==0){
			dao.updateMachineEndtime(map);
		}
		if(i > 0){//end_time 저장
			map.put("component_id", componentList.get(i-1).getComponent_id());
			dao.updateMachineEndtime(map);
		}
		if(i==(componentList.size()-1)){//마지막 제품 시간 저장
			map.put("time", time);
			map.put("component_id", componentList.get(i).getComponent_id());
			dao.updateMachineEndtime(map);
		}
		
	}
	@Override
	public void setMachineBInfo(ArrayList<ComponentDTO> componentList, int i) throws SQLException {
		HashMap map = new HashMap();
		String time = Utility.getNow();
		map.put("time", time);
		map.put("component_id", componentList.get(i).getComponent_id());
		map.put("machine", "machine_b");
		double num = 0;
		num = Math.random()*100;
		
		if(num <= 20){
			componentList.get(i).setIs_fault("Y");
			map.put("is_fault", "Y");
			map.put("faulty_line", "B");
			dao.insertHistory(map);			
		}else{
			componentList.get(i).setIs_fault("N");
			map.put("is_fault", "N");
			map.put("faulty_line", " ");
			dao.insertHistory(map);
		}
		dao.updateComponentInfo(componentList.get(i));
		
		dao.insertMachineB(map);
		if(i==0){
			dao.updateMachineEndtime(map);
		}
		if(i > 0){//end_time 저장
			map.put("component_id", componentList.get(i-1).getComponent_id());
			dao.updateMachineEndtime(map);
		}
		if(i==(componentList.size()-1)){//마지막 제품 시간 저장
			map.put("time", time);
			map.put("component_id", componentList.get(i).getComponent_id());
			dao.updateMachineEndtime(map);
		}
	}
	@Override
	public void setMachineCInfo(ArrayList<ComponentDTO> componentList, int i) throws SQLException {
		HashMap map = new HashMap();
		String time = Utility.getNow();
		map.put("time", time);
		map.put("component_id", componentList.get(i).getComponent_id());
		map.put("machine", "machine_c");
		double num = 0;
		num = Math.random()*100;
		
		if(num <= 20){
			componentList.get(i).setIs_fault("Y");
			map.put("is_fault", "Y");
			map.put("faulty_line", "C");
			dao.insertHistory(map);
		}else{
			map.put("is_fault", "N");
			map.put("faulty_line", " ");
			componentList.get(i).setIs_fault("N");
			dao.insertHistory(map);	
		}
		dao.updateComponentInfo(componentList.get(i));
		
		dao.insertMachineC(map);
		if(i==0){
			dao.updateMachineEndtime(map);
		}
		if(i > 0){//end_time 저장
			map.put("component_id", componentList.get(i-1).getComponent_id());
			dao.updateMachineEndtime(map);
		}
		if(i==(componentList.size()-1)){//마지막 제품 시간 저장
			map.put("time", time);
			map.put("component_id", componentList.get(i).getComponent_id());
			dao.updateMachineEndtime(map);
		}
	}
	@Override
	public void insertComponentNum(String component_name) throws SQLException {
		dao.insertComponentNum(component_name);
	}
	
	public ArrayList<MachineDTO> getAllMachineByName(String machineName) throws SQLException{
		return (ArrayList<MachineDTO>)dao.selectAllMachineByName(machineName);
	}
	@Override
	public ArrayList<MachineDTO> getIsFaultMachineByName(String string) throws SQLException {
		return dao.getIsFaultMachineByName(string);
	}
}
